'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }
  //热门航拍点的请求
  async hotpic() {
    const { ctx } = this;
    //前端传过来的数据
    let pagenumber = ctx.request.query.day
    //后端请求数据库的数据
    // let data = await ctx.service.home.home.index(pagenumber)
    const sql = `select * from hot where page="${pagenumber}"`
    const res = await this.app.mysql.query(sql)
    let data = res
    ctx.body = '1111'
  }
  async photographer() {
    const { ctx } = this
    // let recommend=ctx.params.type
    let recommend = ctx.request.query.type
    const sql = `select *from photographer where type="recommend"`
    const res = await this.app.mysql.query(sql)
    // let data=res
    ctx.body = res
  }
  // 获取自然的数据
  async naturalData() {
    const { ctx } = this;
    let res = ctx.request.body;
    let num = res.pageSize;//查的个数 4
    let start = (res.currentPage - 1) * num;//从第几个开始查
    let sql = `SELECT * FROM graduation.selected WHERE module='${res.module}' order by time desc limit ${start},${num} ;`
    const data = await this.app.mysql.query(sql)
    ctx.body = data;
  }
  // 获取自然的全部数据
  async naturalDatas() {
    const { ctx } = this;
    let res = ctx.request.body;
    let sql = `SELECT * FROM graduation.selected WHERE module='${res.module}'`
    const data = await this.app.mysql.query(sql)
    ctx.body = data;
  }
  async myAgree() {
    const { ctx } = this;
    let res = ctx.request.body;
    let data2
    if (res.noAgree) {
      let sql = `SELECT id FROM graduation.support WHERE user_id=${res.user_id} AND selected_id=${res.id};`;
      let quedata = await this.app.mysql.query(sql);
      let queryId = quedata[0].id;
      let delSql = `DELETE FROM graduation.support WHERE id=${queryId}`;
      data2 = await this.app.mysql.query(delSql);
    } else {
      let sql2 = `INSERT INTO graduation.support SET user_id=${res.user_id},selected_id=${res.id};`;
      data2 = await this.app.mysql.query(sql2);
    }
    let sql = `UPDATE graduation.selected SET agree=${res.agree} WHERE id=${res.id};`;
    let data = await this.app.mysql.query(sql);
    ctx.body = [data, data2];
  }

  async supportData() {
    const { ctx } = this;
    let sql = `SELECT * FROM graduation.support ;`
    const data = await this.app.mysql.query(sql);
    ctx.body = data
  }
}

module.exports = HomeController;
